#!/usr/bin/env bash
set -e

#
# RStudio fork of GWT started with BRANCH off of GWT 2.8.2 plus our changes.
# That is, started life with:
#
#   git branch rstudio/v1.3 tags/2.8.2
#   git checkout rstudio/v1.3
#   git push --set-upstream origin rstudio/v1.3
#
# GWT branches will use the RStudio release names: e.g. rstudio/chocolate-cosmos. Not all
# RStudio releases will have a corresponding GWT branch (we don't change GWT very often).
#
# See further below for instructions on updating the version of GWT used in RStudio.
#

# check out our copy of gwt and build tools
mkdir -p gwtproject
cd gwtproject

set +e

git clone git@github.com:rstudio/gwt.git
(cd gwt && git remote add upstream https://github.com/gwtproject/gwt)
(cd gwt && git pull)

git clone git@github.com:gwtproject/tools.git
(cd tools && git pull)

set -e

# move to gwt directory
cd gwt
git status
git diff --quiet

# get latest main from upstream
git fetch upstream
git checkout main
git merge upstream/main

cat <<- EOF

The GWT sources have been checked out at 'gwtproject/gwt'.

If you're preparing to update the bundled version of GWT in RStudio,
you can take the following steps:

1. Find the branch associated with the most recent RStudio release,
2. Check out that release branch,
3. Create a new branch off that release,
4. Merge the appropriate upstream version of GWT into that branch.

As an example, when we updated from Kousa Dogwood to Cucumber Sunflower:

	cd gwtproject/gwt
	git fetch upstream
	git checkout rstudio/kousa-dogwood
	git checkout -B rstudio/cucumberleaf-sunflower
	git merge 2.12.2

Next, you can try building GWT using:

	# move to GWT directory
	pushd gwtproject/gwt

	# make sure JDK 17 is used; build may fail with newer JDKs
	export JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home

	# execute build; use GWT version appropriate for release of RStudio
	GWT_VERSION=2.12.2
	ant clean dist-dev "-Dgwt.version=${GWT_VERSION}"

	# go home
	popd

If the build succeeds, you can push your newly-created branch. You can now also
begin committing GWT patches to this branch as necessary.

Once you are ready to use this build of GWT with RStudio, you can copy the
built artifacts into the dependency tree. Note that we're assuming you've
already downloaded and installed a previous version of RStudio's GWT tools;
this includes auxiliary libraries used by GWT like 'elemental2'.

	# using this script's directory as the working directory
	rm -rf ../../../dependencies/common/gwtproject/gwt/gwt-rstudio
	cp -R "gwtproject/gwt/build/staging/gwt-${GWT_VERSION}" "../../../dependencies/common/gwtproject/gwt/gwt-rstudio"

Finally, once you're ready to ensure this version of GWT is downloaded and used
by RStudio during the dependency installation process, you can do something
like:

	cd ../../../dependencies/common/gwtproject
	rm -f gwt-*
	tar czf gwt.tar.gz
	aws s3 cp gwt.tar.gz s3://rstudio-buildtools/gwt/gwt-2.12.2-apple-blossom.tar.gz --acl public-read
	rm gwt.tar.gz

and then update the GWT versions used in our various dependency files.

You may also need to update the files at:

	src/gwt/.classpath
	src/gwt/.project

as IDE tools will use these to resolve the Java libraries used by RStudio.

EOF

